<script>
let Token = wx.getStorageSync('userInfo').access_token;
let userInfo = wx.getStorageSync('userInfo');
import * as User_API from '@/api/user.js'
import Vue from 'vue'
export default {
		onLaunch: function() {
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif
			
					// #ifdef MP-WEIXIN || MP-QQ
					Vue.prototype.StatusBar = e.statusBarHeight;
					let capsule = wx.getMenuButtonBoundingClientRect();
					if (capsule) {
						Vue.prototype.Custom = capsule;
						// Vue.prototype.capsuleSafe = uni.upx2px(750) - capsule.left + uni.upx2px(750) - capsule.right;
						Vue.prototype.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					}
					// #endif		
				
			
					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})
			this.autoUpdate()
		},
		onShow: function() {
			// console.log('App Show')
			if(!Token){
				wx.login({
				  success:async(res)=>{
					let params = {
						code: res.code,
						uuid : uni.getStorageSync('uuid')||'21312'
					}
					User_API.AutoLogin(params).then(res =>{
						if(res.id){
							wx.setStorageSync('token',res.access_token);
							wx.setStorageSync('userInfo',res);
							let role = uni.getStorageSync('role')
							if(!role.member_role_id){
								let shop_id = uni.getStorageSync('shop_id') || 2
								User_API.checkRole({shop_id:shop_id}).then(res =>{
									if(res.member_role_id){
										uni.setStorageSync('role',res)
									}
								})
							}else{
								let oldRoleId = role.member_role_id
								let shop_id = uni.getStorageSync('shop_id') || 2
								if(oldRoleId !== uni.getStorageSync('role').member_role_id){
									User_API.checkRole({shop_id:shop_id}).then(res =>{
										if(res.member_role_id){
											uni.setStorageSync('role',res)
										}
									})
								}
							}
						}
					}).catch(()=>{
					})
				  },
				})
			}
		},
		onHide: function() {
			// console.log('App Hide')
		},
		globalData: {
		},
		filters: {
			toFixed:function(x) {
				return parseFloat(x).toFixed(2);
			},
			unixToDate(unix, format) {
			  let _format = format || 'yyyy-MM-dd hh:mm:ss'
			  const d = new Date(unix)
			  const o = {
			    'M+': d.getMonth() + 1,
			    'd+': d.getDate(),
			    'h+': d.getHours(),
			    'm+': d.getMinutes(),
			    's+': d.getSeconds(),
			    'q+': Math.floor((d.getMonth() + 3) / 3),
			    S: d.getMilliseconds()
			  }
			  if (/(y+)/.test(_format)) _format = _format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
			  for (const k in o) if (new RegExp('(' + k + ')').test(_format)) _format = _format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
			  return _format
			}
		},
		methods:{
			autoUpdate() {
			    var self = this
			    // 获取小程序更新机制兼容
			    if (wx.canIUse('getUpdateManager')) {
			      const updateManager = wx.getUpdateManager()
			      //1. 检查小程序是否有新版本发布
			      updateManager.onCheckForUpdate(function(res) {
					  console.log('版本',res)
			        // 请求完新版本信息的回调
			        if (res.hasUpdate) {
			          //检测到新版本，需要更新，给出提示
			          wx.showModal({
			            title: '更新提示',
			            content: '检测到新版本，是否下载新版本并重启小程序？',
			            success: function(res) {
			              if (res.confirm) {
			                //2. 用户确定下载更新小程序，小程序下载及更新静默进行
			                self.downLoadAndUpdate(updateManager)
			              } else if (res.cancel) {
			                //用户点击取消按钮的处理，如果需要强制更新，则给出二次弹窗，如果不需要，则这里的代码都可以删掉了
			                wx.showModal({
			                  title: '温馨提示~',
			                  content: '本次版本更新涉及到新的功能添加，旧版本无法正常访问的哦~',
			                  showCancel:false,//隐藏取消按钮
			                  confirmText:"确定更新",//只保留确定更新按钮
			                  success: function(res) {
			                    if (res.confirm) {
			                      //下载新版本，并重新应用
			                      self.downLoadAndUpdate(updateManager)
			                    }
			                  }
			                })
			              }
			            }
			          })
			        }
			      })
			    } else {
			      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
			      wx.showModal({
			        title: '提示',
			        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
			      })
			    }
			  },
			  /**
			   * 下载小程序新版本并重启应用
			   */
			  downLoadAndUpdate (updateManager){
			    var self=this
			    wx.showLoading();
			    //静默下载更新小程序新版本
			    updateManager.onUpdateReady(function () {
			      wx.hideLoading()
			      //新的版本已经下载好，调用 applyUpdate 应用新版本并重启
			      updateManager.applyUpdate()
			    })
			    updateManager.onUpdateFailed(function () {
			      // 新的版本下载失败
			      wx.showModal({
			        title: '已经有新版本了哟~',
			        content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
			      })
			    })
			  }
		}
	}
</script>

<style lang="scss">
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import "colorui/animation.css";	
	.wq-order-text-orange{
		color:#ff6a22
	}
	.wq-lheight-15{
		line-height: 30upx;
	}
	.bg-gradual-theme{
		background-image: linear-gradient(45deg, $color-main, $color-main);
		color: #ffffff;
	}
	.bg-theme{
		background: $color-main;
		color: #ffffff;
	}
	.text-theme{
		color: $color-main;
	}
	.text-theme,
	.line-theme,
	.lines-theme {
		color: $color-main;
	}
	.line-theme::after,
	.line-theme::after {
		border-color: $color-main;
	}
	.nav-list {
		display: flex;
		flex-wrap: wrap;
		padding: 0px 40upx 0px;
		justify-content: space-between;
	}
	
	.nav-li {
		padding: 30upx;
		border-radius: 12upx;
		width: 45%;
		margin: 0 2.5% 40upx;
		background-image: url(https://cdn.nlark.com/yuque/0/2019/png/280374/1552996358352-assets/web-upload/cc3b1807-c684-4b83-8f80-80e5b8a6b975.png);
		background-size: cover;
		background-position: center;
		position: relative;
		z-index: 1;
	}
	
	.nav-li::after {
		content: "";
		position: absolute;
		z-index: -1;
		background-color: inherit;
		width: 100%;
		height: 100%;
		left: 0;
		bottom: -10%;
		border-radius: 10upx;
		opacity: 0.2;
		transform: scale(0.9, 0.9);
	}
	
	.nav-li.cur {
		color: #fff;
		background: rgb(94, 185, 94);
		box-shadow: 4upx 4upx 6upx rgba(94, 185, 94, 0.4);
	}
	
	.nav-title {
		font-size: 32upx;
		font-weight: 300;
	}
	
	.nav-title::first-letter {
		font-size: 40upx;
		margin-right: 4upx;
	}
	
	.nav-name {
		font-size: 28upx;
		text-transform: Capitalize;
		margin-top: 20upx;
		position: relative;
	}
	
	.nav-name::before {
		content: "";
		position: absolute;
		display: block;
		width: 40upx;
		height: 6upx;
		background: #fff;
		bottom: 0;
		right: 0;
		opacity: 0.5;
	}
	
	.nav-name::after {
		content: "";
		position: absolute;
		display: block;
		width: 100upx;
		height: 1px;
		background: #fff;
		bottom: 0;
		right: 40upx;
		opacity: 0.3;
	}
	
	.nav-name::first-letter {
		font-weight: bold;
		font-size: 36upx;
		margin-right: 1px;
	}
	
	.nav-li text {
		position: absolute;
		right: 30upx;
		top: 30upx;
		font-size: 52upx;
		width: 60upx;
		height: 60upx;
		text-align: center;
		line-height: 60upx;
	}
	
	.text-light {
		font-weight: 300;
	}
	
	@keyframes show {
		0% {
			transform: translateY(-50px);
		}
	
		60% {
			transform: translateY(40upx);
		}
	
		100% {
			transform: translateY(0px);
		}
	}
	
	@-webkit-keyframes show {
		0% {
			transform: translateY(-50px);
		}
	
		60% {
			transform: translateY(40upx);
		}
	
		100% {
			transform: translateY(0px);
		}
	}
	.empty{
	  width: 100%;
	  height: 85vh;
	  display: flex;
	  justify-content: center;
	  align-cartList: center;
	  color: #666;
	  font-size: 28upx;
	  padding-bottom: 80upx;
	}
	.card-category{
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #FFF;
		height: 80upx;
		padding: 0 30upx;
		margin-bottom: 30upx;
		span{
			color: #333;
			font-size: 32upx;
			font-weight: 600;
		}
		p{
			color: #666;
			font-size:26upx;
		}
	}
	.loginBtn{
		background-color: #07C160;
	}
	.title_line_2{
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
	.loading-text {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 60upx;
			color: #979797;
			font-size: 24upx;
		}
</style>
